package org.ptkparty.client.ui;

import java.util.Set;

import org.ptkparty.client.Context;
import org.ptkparty.client.model.ChampionshipProxy;

import com.google.gwt.core.client.GWT;
import com.google.gwt.dom.client.AnchorElement;
import com.google.gwt.dom.client.Document;
import com.google.gwt.dom.client.ScriptElement;
import com.google.gwt.event.dom.client.ChangeEvent;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiConstructor;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.Widget;

public class Menu extends Composite {

	private static MenuUiBinder uiBinder = GWT.create(MenuUiBinder.class);

	interface MenuUiBinder extends UiBinder<Widget, Menu> {
	}

	@UiField
	AnchorElement playersBtn;

	@UiField
	AnchorElement championshipsBtn;

	@UiField
	AnchorElement prepareGamesBtn;

	@UiField
	AnchorElement addGameBtn;

	@UiField
	AnchorElement showGamesBtn;

	@UiField
	AnchorElement resultBtn;

	@UiField
	AnchorElement statsBtn;

	@UiField
	AnchorElement scalesBtn;

	@UiField
	AnchorElement profilBtn;

	@UiField
	AnchorElement placesBtn;

	@UiField
	ListBox championshipList;

	@UiHandler("championshipList")
	public void onChampionshipChange(ChangeEvent event) {
		String selectedChampionship = null;
		if (championshipList.getSelectedIndex() != -1) {
			selectedChampionship = championshipList.getValue(championshipList.getSelectedIndex());
		}

		Context.get().setCurrentChampionship(selectedChampionship);

		if (selectedChampionship != null) {
			playersBtn.setAttribute("href", "#PlayersPlace:" + selectedChampionship);
			championshipsBtn.setAttribute("href", "#ChampionshipsPlace:" + selectedChampionship);
			prepareGamesBtn.setAttribute("href", "#PrepareGamesPlace:" + selectedChampionship);
			addGameBtn.setAttribute("href", "#AddGamePlace:" + selectedChampionship);
			showGamesBtn.setAttribute("href", "#ShowGamesPlace:" + selectedChampionship);
			resultBtn.setAttribute("href", "#ResultsPlace:" + selectedChampionship);
			statsBtn.setAttribute("href", "#StatsPlace:" + selectedChampionship);
			scalesBtn.setAttribute("href", "#ScalesPlace:" + selectedChampionship);
			profilBtn.setAttribute("href", "#ProfilPlace:" + selectedChampionship + "&" + Context.get().getCurrentPlayer().getId());
			placesBtn.setAttribute("href", "#PlacesPlace:" + selectedChampionship);
		} else {
			playersBtn.setAttribute("href", "#");
			championshipsBtn.setAttribute("href", "#");
			prepareGamesBtn.setAttribute("href", "#");
			addGameBtn.setAttribute("href", "#");
			showGamesBtn.setAttribute("href", "#");
			resultBtn.setAttribute("href", "#");
			statsBtn.setAttribute("href", "#");
			scalesBtn.setAttribute("href", "#");
			profilBtn.setAttribute("href", "#");
			placesBtn.setAttribute("href", "#");
		}

		if (!Context.get().getCurrentPlayer().isAdmin()) {
			playersBtn.setAttribute("href", "#");
			playersBtn.setClassName("disabled");
		}
	}

	@UiConstructor
	public Menu() {
		initWidget(uiBinder.createAndBindUi(this));

		Document doc = Document.get();
		ScriptElement script = doc.createScriptElement();
		script.setSrc("https://apis.google.com/js/plusone.js");
		script.setType("text/javascript");
		script.setLang("javascript");
		doc.getBody().appendChild(script);

		script = doc.createScriptElement();
		script.setSrc("http://connect.facebook.net/fr_FR/all.js#xfbml=1&appId=448623828558120");
		script.setType("text/javascript");
		script.setLang("javascript");
		doc.getBody().appendChild(script);

		Set<ChampionshipProxy> championshipList = Context.get().getChampionshiplist();
		if (championshipList != null && !championshipList.isEmpty()) {
			int selectedIndex = 0, i = 0;
			for (ChampionshipProxy championship : championshipList) {
				if (championship.getId() == Context.get().getCurrentChampionship()) {
					selectedIndex = i;
				}
				this.championshipList.addItem(championship.getName(), championship.getId());
				i++;
			}
			this.championshipList.setSelectedIndex(selectedIndex);
		}
		onChampionshipChange(null);
	}

}
